<extend name="Public:template" />
<block name="page_header">
	<include file="Public:header_with_title"/>
</block>
<block name="page_body">
	<div class="row mt20">
		<div class="span20" id="stepOrder">
			<ul class="nav nav-tabs">
				<li class="active">
					<a href="#"><h4>第一步：我的购物车</h4></a>
				</li>
				<li class="disabled"><a href="#"><h4>第二步：选择配送地址</h4></a></li>
				<li class="disabled"><a href="#"><h4>第三步：等待收货</h4></a></li>
			</ul>
		</div>
		<div class="span20 mt-10">
			<table class="table table-bordered prodList">
				<tr class="alert-info">
					<th class="span2">商品清单</th>
					<th class="span4">商品名称</th>
					<th class="span3">规格单位</th>
					<th class="span3">价格</th>
					<th class="span3">数量</th>
				</tr>
				<empty name="cartItems">
					<tr>
						<td colspan="5">
							购物车中还没有商品 
							<a href="{:U('Index/index')}" class="btn btn-success" role="button"><span class="icon-white icon-search"></span>继续购物</a>						</td>
					</tr>
				</empty>
				<volist name="cartItems" id="val">
					<tr class="prodItem" id="{$val.prod_id}">
						<td>
							<img style="width:60px;height:60px" src="{$val.prod_id|m_prod_pic}" alt="{$val.name}">
						</td>
						<td>{$val.name}<notempty name="val.stock_message"> <span class="label label-danger">{$val.stock_message}</span></notempty></td>
						<td>{$val.spec|m_prod_spec=$val['spec_qty'],$val['spec_unit']}</td>	
						<td id="itemPriceReal{$val.prod_id}" price_real="{$val.price_real}">¥{$val.price_real}</td>
						<td>
							<div class="input-prepend input-append ">
								<button class="btn" title="delete" onclick="addToCart2('{$val.prod_id}','0','2');"><i class="icon-trash"></i></button>
								<button type="button" class="btn"  onclick="addToCart2('{$val.prod_id}','-1','1');">-</button>
								<input type="text"  class="input-micro" id="itemNum{$val.prod_id}" name="itemNum[]" value="{$val.num}" onChange="addToCart2('{$val.prod_id}',this.value,'2')"/>
								<button type="button" class="btn"  onclick="addToCart2('{$val.prod_id}','1','1');">+</button>
							</div>
						</td>
					</tr>
				</volist>
			</table>
		</div>
		<div  class="span20">
			<!--统计信息-->
			<div class="cartTotal mt5">
				<p class="text-right mt5 price" id="suminfo">
					您购买了<span class="text-danger" id="total_item" >{$cartPrice.total_item}</span>种商品，总共<span class="text-danger"  id="total_num">{$cartPrice.total_num}</span>件，商品总价：<span class="now" id="total_amt">¥{$cartPrice.total_amt|number_format=###, 2,'.',''}</span>
		    	</p>
		    	<p class="text-right mt5">
		    		(订单金额超过59元免运费)
		    	</p>
			</div>
		</div>
		<if condition="$cartPrice.total_amt gt 0">
			<!--操作按钮-->
			<div class="span20">
				<a href="{:U('Index/index')}" class="btn btn-success" role="button"><span class="icon-white icon-search"></span>继续购物</a>
				<a href="#" class="btn" role="button" onClick="doEmptyCart()"><span class="icon icon-trash"></span>清空购物车</a>

				<a href="{:U('CustOrder/showConfirm')}" class="btn btn-large btn-warning  pull-right" role="button">选择配送地址<span class="icon-white icon-chevron-right"></span></a>
			</div>
		</if>
	</div>
</block>
<block name="script_footer">
<script type="text/javascript">
function addToCart2(prod_id,num,edittype){
	//document.location.href = "/cart/doadd.html?prod_id="+prod_id+"&num="+num+"&edittype="+edittype;
	var currNum = parseInt( $("#itemNum"+prod_id).val());
	var leftNum = currNum+parseInt(num);
	if(currNum<=0||(edittype=='1' && leftNum<=0)||(edittype=='2' && num<=0)){
		if(!confirm('确定将该商品从购物车中移除？')){
			return;
		}
	}
	$.ajax({
		'async': true, 
        'url': "/cart/doadd.html",
        'dataType': "json",
        'data': {'prod_id':prod_id,'num':num,'edittype':edittype},
        'success': function (data) {
        	if(data['error']=='0'){
	        	if(data['message']!=''){
	        		alert(data['message']);
	        	}
	        	var cart_num = data['cart_num'];
	        	var item_num = data['item_num'];
	        	$("#cartNum").html(cart_num);
	        	$("#itemNum"+prod_id).val(item_num);
	        	if(item_num<=0){
	        		$("#"+prod_id).remove();
	        	}
	        	if(cart_num<=0){
	        		$(".cartAction").hide();
	        	}
	        	calcPrice();
        	}else{
        		alert(data['message']);
        	}
        }
    });
}
function calcPrice(){
	var totalItem = 0;
	var totalAmt = 0;
	var totalNum = 0;
	$(".prodList .prodItem").each(function(){
		var prod_id = $(this).attr("id");
		var item_num = parseInt($("#itemNum"+prod_id).val());
		var item_price = parseFloat($("#itemPriceReal"+prod_id).attr("price_real"));
		totalAmt += item_price*item_num;
		totalNum += item_num;
		totalItem++;
	});
	$("#total_item").html(totalItem);
	$("#total_num").html(totalNum);
	$("#total_amt").html("¥"+totalAmt.toFixed(2));
	
}
function doEmptyCart(){
	if(confirm('确定清空购物车?')){
		document.location.href="/Cart/doEmpty.html";
	}
}
</script>
</block>